<template>
  <div class="login">
    <s-header
      :name="type == 'login' ? '登录' : '注册'"
      :back="'/home'"
    ></s-header>
    <img
      class="logo"
      src="https://s.yezgea02.com/1604045825972/newbee-mall-vue3-app-logo.png"
      alt=""
    />
    <div v-if="formState.type == 'login'" class="login-body login">
      <van-form @submit="onSubmit">
        <van-field
          v-model="loginState.userName"
          name="userName"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="loginState.password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <!-- <van-field
          center
          clearable
          label="验证码"
          placeholder="输入验证码"
          v-model="loginState.verify"
        >
          <template #button>
            <vue-img-verify ref="verifyRef" />
          </template>
        </van-field> -->
        <div style="margin: 16px">
          <div class="link-register" @click="toggle('register')">立即注册</div>
          <van-button round block color="#1baeae" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
    <div v-else class="login-body register">
      <van-form @submit="onSubmit">
        <van-field
          v-model="registerState.userName"
          name="userName"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="registerState.password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-model="registerState.name"
          name="name"
          label="姓名"
          placeholder="姓名"
          :rules="[{ required: true, message: '请填写姓名' }]"
        />

        <van-field
          v-model="registerState.code"
          name="推荐码"
          label="推荐码"
          placeholder="推荐码"
          :rules="[{ required: true, message: '请填写推荐码' }]"
        />
        <!-- <van-field
          center
          clearable
          label="验证码"
          placeholder="输入验证码"
          v-model="state.verify"
        >
          <template #button>
            <vue-img-verify ref="verifyRef" />
          </template>
        </van-field> -->
        <div style="margin: 16px">
          <div class="link-login" @click="toggle('login')">已有登录账号</div>
          <van-button round block color="#1baeae" native-type="submit"
            >注册</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import sHeader from "@/components/SimpleHeader/Index.vue";
import vueImgVerify from "@/components/VueImageVerify/Index.vue";
import { login, register } from "@/service/user";
import { setLocal } from "@/common/js/utils";
import md5 from "js-md5";
import { showSuccessToast, showFailToast } from "vant";
import { useUserStore } from "../../stores";
import { useRouter } from "vue-router";

import "./index.less";

const userStore = useUserStore();
const router = useRouter();
// const verifyRef = ref(null);
const formState = reactive({
  type: "login",
});

const loginState = reactive({
  userName: "",
  password: "",
});

const registerState = reactive({
  userName: "",
  password: "",
  name: "",
  code: "hello",
  avatar: "",
});

// 切换登录和注册两种模式
const toggle = (v) => {
  formState.type = v;
  //   state.verify = "";
};

// 提交登录或注册表单
const onSubmit = async (values) => {
  //   state.imgCode = verifyRef.value.state.imgCode || "";
  //   if (state.verify.toLowerCase() != state.imgCode.toLowerCase()) {
  //     showFailToast("验证码有误");
  //     return;
  //   }
  if (formState.type == "login") {
    const { data } = await login({
      ...loginState,
      ...{ password: md5(loginState.password) },
    });
    userStore.setUserData(data);
    setLocal("token", data.token);
    userStore.info();
    router.replace({
      name: "home",
    });
  } else {
    await register({
      ...registerState,
      ...{ password: md5(registerState.password) },
    });
    showSuccessToast("注册成功");
    userStore.setUserData(data);
    setLocal("token", data.token);
    userStore.info();
    router.replace({
      name: "home",
    });
    formState.type = "login";
  }
};
</script>

<style lang="less">
.login {
  .logo {
    width: 120px;
    height: 120px;
    display: block;
    margin: 80px auto 20px;
  }
  .login-body {
    padding: 0 20px;
  }
  .login {
    .link-register {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
  .register {
    .link-login {
      font-size: 14px;
      margin-bottom: 20px;
      color: #1989fa;
      display: inline-block;
    }
  }
  .verify-bar-area {
    margin-top: 24px;
    .verify-left-bar {
      border-color: #1baeae;
    }
    .verify-move-block {
      background-color: #1baeae;
      color: #fff;
    }
  }
  .verify {
    > div {
      width: 100%;
    }
    display: flex;
    justify-content: center;
    .cerify-code-panel {
      margin-top: 16px;
    }
    .verify-code {
      width: 40% !important;
      float: left !important;
    }
    .verify-code-area {
      float: left !important;
      width: 54% !important;
      margin-left: 14px !important;
      .varify-input-code {
        width: 90px;
        height: 38px !important;
        border: 1px solid #e9e9e9;
        padding-left: 10px;
        font-size: 16px;
      }
      .verify-change-area {
        line-height: 44px;
      }
    }
  }
}
</style>
